"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index$1 = require("../config/index.js");
const request = require("request");
const utils = require("utils");
const index = require("./requestSecretData/index.js");
const formatterContent = require("./formatterContent.js");
const db = uniCloud.database();
const collection = db.collection("token-valid");
const DefaultDingDingDocToken = "NjAzMjA1NzIwZGcFbLjwAZPVCQfFvnWicjQhefhmWdmU";
const DingDingTokenType = "dingding-doc";
async function getDbCache() {
  let [err, res] = await utils.util.awaitWrap(
    collection.where({
      "token-type": DingDingTokenType
    }).limit(1).get()
  );
  if (err) {
    return [new utils.InsideErr(err), void 0];
  }
  let { data = [] } = res || {};
  return [void 0, data[0]];
}
async function changeDingToken(valid, docId) {
  if (docId) {
    await collection.doc(docId).update({
      valid,
      update: utils.util.baseDateDes(/* @__PURE__ */ new Date())
    });
  }
}
async function getDingDingToken() {
  let [err, data] = await getDbCache();
  if (!utils.util.nilValue(err)) {
    return [err, void 0];
  }
  let dingToken = {
    valid: false
  };
  if (utils.util.nilValue(data)) {
    let res = await collection.add({
      token: DefaultDingDingDocToken,
      "token-type": DingDingTokenType,
      valid: true,
      update: utils.util.baseDateDes(/* @__PURE__ */ new Date())
    });
    dingToken = {
      token: DefaultDingDingDocToken,
      valid: true,
      id: res.id
    };
  } else {
    dingToken = {
      token: data.token,
      valid: data.valid,
      id: data._id
    };
  }
  if (utils.util.nilValue(dingToken.token)) {
    return [new utils.BaseErr(-4001, "dingding-token获取失败"), void 0];
  }
  return [void 0, dingToken];
}
async function requestDocData(token) {
  const DingDocApi = index$1.dingApi;
  const postData = {
    pageMode: 2,
    orgGrayKeys: ["enable_notable_frontend"],
    enableSlice: true,
    anchor: {
      needSheetSkeleton: true
    }
  };
  const headers = {
    ...index$1.dingHeader,
    cookie: `doc_atoken=${token}`,
    pragma: "no-cache"
  };
  return await request.postRequest(DingDocApi)(postData, {
    // enableProxy: true,
    // rejectUnauthorized: false,
    // proxy: "http://127.0.0.1:8899",
    contentType: "json",
    headers
  });
}
async function getDocInfo(docData, tokenId) {
  var _a;
  let { status, data } = docData || {};
  if (status !== 200) {
    await changeDingToken(false, tokenId);
    return [new utils.BaseErr(-4007, `token 可能失效了,  doc数据状态码 ${status}`), void 0];
  }
  let checkpoint = ((_a = data == null ? void 0 : data.documentContent) == null ? void 0 : _a.checkpoint) ?? {};
  let { secretInfo, ecpOssUrl, content } = checkpoint;
  if (!utils.util.nilValue(secretInfo) && !utils.util.nilValue(ecpOssUrl)) {
    return await index.toRequestSecretData(secretInfo, ecpOssUrl);
  }
  if (!utils.util.nilValue(content)) {
    return formatterContent.formatterContent(content);
  }
  return [new utils.BaseErr(-4003, "返回的数据不对"), void 0];
}
async function getDocData() {
  let [err, token] = await getDingDingToken();
  if (!utils.util.nilValue(err)) {
    return [err, void 0];
  }
  if ((token == null ? void 0 : token.valid) !== true || utils.util.nilValue(token == null ? void 0 : token.token)) {
    return [new utils.BaseErr(-4009, "token  已失效"), void 0];
  }
  let [doc_err, doc_data] = await requestDocData(token.token);
  if (!utils.util.nilValue(doc_err)) {
    return [doc_err, void 0];
  }
  return await getDocInfo(doc_data, token.id);
}
exports.getDocData = getDocData;
